/**
  * @Author: songgh
  * @Date: 2021-09-20 14:41:08
  * @LastEditors: songgh
  * @LastEditTime: 2021-09-20 15:41:08
  * @Description: request api
  */

import { GET, POST, ILoginData, IUploadData, IPlannerParams, IServiceSearchParmas, ITokenLoginParams } from "./index";
import { townLayer } from '../../public/static/app-config.json';
import Query from "@arcgis/core/rest/support/Query";
import QueryTask from "@arcgis/core/tasks/QueryTask";

export function plannerSearch(params: IPlannerParams) {
    return GET("/rest/bms/v1/planner/info/search", params);
}

export function serviceSearch(params: IServiceSearchParmas) {
    return GET("/rest/bms/v1/planner/serviceinfo/search", params);
}

export function upload(data: IUploadData) {
    const paramsArr = (Object.keys(data).filter(key => key !== 'imgs')).map(key => { if (data[key]) { return `${key}=${data[key]}` } });
    return POST(`/rest/bms/v1/planner/serviceinfo/upload?${paramsArr.join('&')}`, data.imgs);
}

export function rsaLogin(params: ILoginData) {
    return POST(`/rest/oms/public/client-account/v1/login`, params);
}

export function getPublickey() {
    return GET("/rest/oms/public/security/publickey");
}

export function loginByToken(params:ITokenLoginParams){
    return GET("/LoginByToken/token",params);
}

export async function getRegionData() {
    let regionData = {};
    try {
        let query = new Query();
        query.where = `1=1`;
        query.outFields = ["*"];
        let queryTask = new QueryTask({
            url: `${townLayer.url}/${townLayer.layerID}`
        });
        const { features } = await queryTask.execute(query);
        features.forEach(item => {
            const {attributes:{quxian,zhenjie}} = item;
            if(regionData[quxian]){
                regionData[quxian].push(zhenjie)
            }else{
                regionData[quxian] = [zhenjie];
            }
        });

    } catch (error) {
        console.error(error);
    }
    return regionData;
}
